<?php
/**
 * User: xuanbang.wen
 * Date: 2019/8/19
 */

namespace App\Web\Model;


use EchoPHP\Facades\Db;

class CaseOperator extends Model {
    private $tablename = 'CaseOperator';

    public function getOpr($caseid) {
        $sql = 'select u.userid,u.cname,isowner,issys from ' . $this->tablename . ' as co
        left join User as u on co.oprid = u.userid
        where co.caseid = ? and co.status = ? order by isowner desc,issys desc';
        return Db::select($sql, [$caseid, '01']);
    }

    public function getCase($oprid) {
        $sql = 'select caseid from ' . $this->tablename .
            ' where oprid = ? and status = ?';
        return Db::select($sql, [$oprid, '01']);
    }

    /**
     * 判断操作人是否具有职位操作权限
     * @param $oprId
     * @param $caseId
     * @return bool
     */
    public function chkCaseOprRight($oprId, int $caseId) {
        $sql = 'SELECT caseid FROM ' . $this->tablename . ' WHERE oprid=? AND status=? AND caseid=?';
        $var = [$oprId, '01', $caseId];
        $result = Db::selectOne($sql, $var);
        return !empty($result);
    }

    /**
     * 判断是否具备职位操作权限(多个职位)
     * @param $oprId
     * @param array $caseIds
     * @return array
     */
    public function chkCasesRight($oprId, array $caseIds) {
        $inFlag = substr(str_repeat('?,', count($caseIds)), 0, -1);
        $sql = 'SELECT caseid FROM ' . $this->tablename . ' WHERE oprid=? AND status=? AND caseid IN(' . $inFlag . ')';
        $var = array_merge([$oprId, '01'], $caseIds);
        $result = Db::select($sql, $var);
        $hasRightCases = [];
        foreach ($result as $item) {
            $hasRightCases[] = $item['caseid'];
        }
        $noRightCases = array_diff($caseIds, $hasRightCases);
        return [
            'hasright' => empty($noRightCases) ? true : false,
            'norightcases' => $noRightCases
        ];
    }

    /**
     * 检查操作人是否有职位的操作权限
     * @param $oprId
     * @param $caseIds
     * @return array
     */
    public function chkMultiCaseOprRight($oprId, array $caseIds) {
        $inFlag = substr(str_repeat('?,', count($caseIds)), 0, -1);
        $sql = 'SELECT caseid,subid,projectid FROM ' . $this->tablename . ' WHERE oprid=? AND status=? AND caseid IN(' . $inFlag . ')';
        $var = array_merge([$oprId, '01'], $caseIds);
        $result = Db::select($sql, $var);
        if(empty($result) || count($result)<1){
            return array();
        }
        foreach ($result as $item) {
            $return[$item['caseid']] = $item;
        }
        return $return;
    }

    /**
     * 判断操作人是否具有操作职位下简历权限，只有进行中的职位可以操作简历
     * @param $oprId
     * @param array $caseIds
     * @return bool
     */
    public function chkMultiCaseResumeOprRight($oprId, array $caseIds) {
        $sql = 'select count(DISTINCT a.caseid) as count from SCase a left JOIN CaseOperator b on a.caseid=b.caseid WHERE a.`status`=? AND b.`status`=? AND b.oprid=? and a.caseid IN(' . mergeArrSql($caseIds) . ')';
        $var = array_merge(['01', '01', $oprId,], $caseIds);
        $result = Db::selectOne($sql, $var);
        return $result['count'] == count($caseIds);
    }

    /**
     * Notes:我的所有职位
     * @param $userId
     * @param string $field
     * @return array
     */
    public function myAllCase($userId, $field = 'subid,projectid,caseid'){
        $sql = 'select ' . $field . ' from CaseOperator where oprid = ? and status = ?';
        return Db::select($sql, [$userId, '01']);
    }


    /**
     * Notes:团队下是否有此职位执行人
     * @param $teamCode
     * @param $caseId
     * @return bool
     */
    public function chkTeamCaseOprRight($teamCode, $caseId) {
        $sql = 'select userid from User where teamcode like ? and status = ?';
        $teamMember = Db::select($sql, [$teamCode, '01']);
        if (!$teamMember) {
            return false;
        }
        $userIds = array_column($teamMember, 'userid');
        $sql = 'SELECT 1 FROM ' . $this->tablename . ' WHERE caseid=? AND status=? AND oprid IN(' . mergeArrSql($userIds) . ')';
        $result = Db::selectOne($sql, array_merge([$caseId, '01'], $userIds));
        return !empty($result);
    }
}